#! /bin/sh

#####################################
# PREPARE variables:
#
app=$1
app_vcomp="$app"Contents/vcomponents


#####################################
# COPY files of vcomponents into APP.
#####################################

if [ ! -d "$app_vcomp" ] ; then
	mkdir "$app_vcomp"
fi

if [ ! -e "$app_vcomp"/libvshared_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvshared_fat_release.dylib 	"$app_vcomp"
fi

if [ ! -e "$app_vcomp"/libvkernel_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvkernel_fat_release.dylib 	"$app_vcomp"
fi

if [ ! -e "$app_vcomp"/libvclient_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvclient_fat_release.dylib 	"$app_vcomp"
fi

#
# Copy Folders:
#
if [ ! -d "$app_vcomp"/VSDK.framework ] ; then
	cp -R /usr/local/lib/vcomponents/VSDK.framework				"$app_vcomp"
fi

cp -R /usr/local/lib/vcomponents/vresources 					"$app_vcomp"


#####################################
# CHANGE executable_path for engine dylibs:
#####################################

	# change id of VSHARED
	install_name_tool -id "@executable_path/../vcomponents/libvshared_fat_release.dylib" "$app_vcomp"/libvshared_fat_release.dylib

	# change id of VKERNEL
	install_name_tool -id "@executable_path/../vcomponents/libvkernel_fat_release.dylib" "$app_vcomp"/libvkernel_fat_release.dylib

	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib"   "@executable_path/../vcomponents/libvshared_fat_release.dylib" 	"$app_vcomp"/libvkernel_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/VSDK.framework/Versions/A/VSDK" "@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" 	"$app_vcomp"/libvkernel_fat_release.dylib

	# change id of VCLIENT
	install_name_tool -id "@executable_path/../vcomponents/libvclient_fat_release.dylib" "$app_vcomp"/libvclient_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" "@executable_path/../vcomponents/libvshared_fat_release.dylib" "$app_vcomp"/libvclient_fat_release.dylib


	# change if of VSDK.framework
    install_name_tool -id "@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" "$app_vcomp"/VSDK.framework/Versions/A/VSDK
    
    
#####################################
# now we need update yet App/Contents/Frameworks/V4RB.rbx_0_xxx.dylib
# REALBasic can assign any digits xxx, so we use globbing (regex) here to locate file.
#####################################
app_frmws="$app"Contents/Frameworks

for file in "$app_frmws"/V4RB*
do
	v4rb_dylib=$file
	break
done

	#
	# now we can change paths:
	#
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" 	"@executable_path/../vcomponents/libvshared_fat_release.dylib" 		"$v4rb_dylib"
	install_name_tool -change "/usr/local/lib/vcomponents/libvkernel_fat_release.dylib" 	"@executable_path/../vcomponents/libvkernel_fat_release.dylib" 		"$v4rb_dylib"
	install_name_tool -change "/usr/local/lib/vcomponents/libvclient_fat_release.dylib" 	"@executable_path/../vcomponents/libvclient_fat_release.dylib" 		"$v4rb_dylib"

	install_name_tool -change "/usr/local/lib/vcomponents/VSDK.framework/Versions/A/VSDK" 	"@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" 	"$v4rb_dylib"
